DirectoryView = function(path) {
	this.file = new MVC.JFile(path);
	this.path = path;
};

DirectoryView.prototype = {
	createElement : function(){
		var div = document.createElement('div');
		div.id = this.path;
		div.innerHTML = this.file.file_and_extension();
		div.className = 'directory';
		div.setAttribute('type', 'directory')	;
		return div;
	},
	appendAsChildOf : function(element){
		this.element = element.appendChild( this.toElement() );
		//new window[this.className.capitalize()+'MVC.Controller']().attach_event_handlers_to(this.element);
		return this.element;
	},
	toElement : function(){
		if(this.element) return this.element;
		this.element = this.createElement();
		return this.element;
	},
	insertBefore : function(element){
		this.element = element.parentNode.insertBefore(this.toElement() , element );
		//new window[this.className.capitalize()+'MVC.Controller']().attach_event_handlers_to(this.element);
		return this.element;
	}
};

DirectoryView.insert = function(path, files_dom, files_container)
{
	//if(MVC.$E(path)) return;
	var view = new DirectoryView(path);
	if(!files_container)
		files_container = view.file.container();
	var files = files_container.childNodes;
	//insert form in appropriate place

	for(var i = 0; i< files.length; i++)
	{
		var file = files[i];
		var fd = new FileData(file);
		
		if(view.file.file_and_extension() <  fd.name || fd.type == 'file') {
			var folder = view.insertBefore(file);
			var ps = new MVC.Controller.Params({ element: folder });
			MVC.Controller.dispatch('directories', 'load_directory', ps);
			files_container.insertBefore( Container( files_dom, path), folder.next()  );
			return folder;
		}
	}
	var folder = view.appendAsChildOf(files_container);
	//var ps = new MVC.Controller.Params({ element: folder });
	//MVC.Controller.publish('loaded_directory', ps);
	files_container.appendChild( Container( files_dom, path) );
	return folder;
};
DirectoryView.rename = function(object){
	var folder_el = MVC.$E(object.from_path);
	var old_container = folder_el.parentNode;
	var files_dom = folder_el.next().remove();
	folder_el.remove(); 
	DirectoryView.insert(object.to_path, files_dom); // inserts file into main dir
	DirectoryView.update(object);
	var parent = MVC.$E(object.to_path).parentNode;
	
	if(old_container.childNodes.length == 0){//for IE, and has to be here 
		old_container.style.display = 'none'; // in case you are just renaming in the same folder
	}
};
/**
 * Creates all the folder and file elements in a folder from the data returned by the request
 * @param {Object} object that contains the folder info that will be updated
 */
DirectoryView.update = function(object, blinding) {
	var folder_element = (object.directory !='/' ? MVC.$E(object.directory).next() : MVC.$E('/').next() );
	object.path_prepend = (object.directory !='/' ? object.directory+'/' : '/' );
	var sorted_files = object.files.sort(File.sort);
	
	var reverse =DirectoryView.clean(folder_element, sorted_files);

	for(var i = 0; i < reverse.length; i++)
	{
		var file = reverse[i];
		if(file.name == '.' || file.name == '..' || file.name.indexOf('->') != -1) continue;
		
		if(file.type =='file'){
			FileView.insert(object.path_prepend+file.name, folder_element);
    	}else{
			var dir_el = DirectoryView.insert(object.path_prepend+file.name, null, folder_element);
    	}
	}
	//if(!blinding) 
        folder_element.style.display ="";
//	/Effect.BlindDown(folder_el, {duration: object.files.length / 30 });
};

// basically goes to that folder and removes anything that no longer exists
//takes a path/id and an array of files
//this should be order new_files + old files
//returns what we actually need returned
DirectoryView.clean = function(folder_element, new_files) {

	var child_els = folder_element.childNodes; //assume nodes in order
	var newlist = [];
	var current = 0;

	if(child_els.length == 0) return new_files;
	for(var i = 0 ; i < child_els.length; i++){ //go through the current ones
		var file = child_els[i];
		var fd = new FileData(file);
		var type = file.getAttribute('type');
		if(! type) continue;
		var found = false;
		while(current < new_files.length){ // increment the new ones, see if the thing should still be here
			var newfile = new_files[current];
			if(newfile.name == '.' || newfile.name == '..') {
				current++;
				continue;
			} 
			if(newfile.name == fd.name && type == newfile.type){
				 current++;
				 found = true; 
				 break; 
			}
			if(type == 'file' && newfile.type == 'directory') break;
			if(newfile.name > fd.name) break;
			newlist.unshift(newfile);
			current++;
		}
		if(!found) file.remove();
	}
	while(current < new_files.length){
		var newfile = new_files[current];
		newlist.push(newfile);
		current++;
	}
	
	
	return newlist;
};